package com.chen.blog.controller;


import com.chen.blog.common.api.CommonResult;
import com.chen.blog.model.Tag;
import com.chen.blog.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "标签管理 TagController")
@RestController
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @ApiOperation("新增标签")
    @PostMapping
    public CommonResult add(@RequestBody Tag tag) {
        return tagService.add(tag);
    }

    @ApiOperation("删除标签")
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return tagService.deleteByIds(ids);
    }

    @ApiOperation("更新标签")
    @PutMapping
    public CommonResult update(@RequestBody Tag tag) {
        return tagService.updateName(tag);
    }

    @ApiOperation("检索标签")
    @GetMapping("/{currentPage}/{pageSize}")
    public CommonResult get(@PathVariable(value = "currentPage") Integer currentPage, @PathVariable(value = "pageSize") Integer pageSize,
                            Tag tag) {
        return tagService.getByParamAndPage(currentPage, pageSize, tag);
    }

    @ApiOperation("检索标签何其对应的博客数量")
    @GetMapping("/blog")
    public List<Map<String, Object>> getBlogCountByTag() {
        return tagService.getBlogCountByTag();
    }

    @ApiOperation("检索指定标签下的博客")
    @GetMapping("/blog/{id}")
    public List<Map<String, Object>> getBlogByTag(@PathVariable Integer id) {
        return tagService.getBlogByTag(id);
    }
}
